/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;

using Gtk;

using DCSharp.Backend.Objects;

namespace DCSharp.Gui
{
	public class SearchResultStore : TreeStore
	{
		public enum Column {
			Object,
			Name,
			Directory,
			Bytes,
			Size,
			User,
			Slots,
			FreeSlots,
			Connection,
			TTH
		};

		private Dictionary<string, TreeIter> hashToIter;

		public SearchResultStore() : base(typeof(object), typeof(string),
			typeof(string), typeof(long), typeof(string), typeof(string),
			typeof(string), typeof(double), typeof(string), typeof(string))
		{
			hashToIter = new Dictionary<string, TreeIter>();
		}

		#region Methods

		public TreeIter AddResult(SearchResult result)
		{
			string file = StringUtil.GetFileName(result.Path);
			string directory = StringUtil.GetDirectoryName(result.Path);
			string size = Util.FormatFileSize(result.Size);
			string slots = result.FreeSlots + "/" + result.Slots;
			double freeSlots = (double)result.FreeSlots / result.Slots;

			// FIXME: Set the connection type.
			TreeIter iter;
			if (result.TTH != null && hashToIter.ContainsKey(result.TTH))
			{
				TreeIter parent = hashToIter[result.TTH];
				iter = AppendValues(parent, result, file, directory,
					result.Size, size, result.User.Nick, slots, freeSlots,
					null, result.TTH);
					//result.User.Connection, result.TTH);
			}
			else
			{
				iter = AppendValues(result, file, directory,
					result.Size, size, result.User.Nick, slots, freeSlots,
					null, result.TTH);
					//result.User.Connection, result.TTH);

				if (result.TTH != null)
				{
					hashToIter.Add(result.TTH, iter);
				}
			}
			return iter;
		}

		public new void Clear()
		{
			hashToIter.Clear();
			base.Clear();
		}

		#endregion
	}
}
